<template>
    <el-container>
        <el-aside :width="`${appStore.menuCollapse ? 0.64 : 2.2}rem`" style="height: 100vh">
            <div class="side-part">
                <div class="title" :class="{ callapse: appStore.menuCollapse }">
                    <img class="sz-30" src="_/assets/imgs/side_menu_logo_small.png" />
                    <span v-if="!appStore.menuCollapse" class="ml-10 title-text fs-20">国控物流TMS</span>
                </div>
                <el-menu :default-active="appStore.defaultActive" text-color="#fff" background-color="#000"
                    class="el-menu-vertical" :collapse="menuCollapse" router @open="handleOpen" @close="handleClose">
                    <el-menu-item index="/home">
                        <icon-svg name="dot"></icon-svg>
                        <template #title>
                            <span>首页</span>
                        </template>
                    </el-menu-item>
                    <side-menu v-if="menus.length" :menus="menus" />
                </el-menu>
            </div>

        </el-aside>
        <el-container>
            <el-header>
                <header-view />
            </el-header>
            <el-main>
                <div class="router-tabs">
                    <el-tabs v-model="currentRouteName" type="card" closable class="header-tabs" @tab-click="tabClick"
                        @tab-remove="tabClose">
                        <el-tab-pane key="Home" label="首页" name="Home" :disabled="true">
                        </el-tab-pane>
                        <el-tab-pane v-for="tag in appStore.storeRouters" :key="tag.name" :label="tag.meta.title"
                            :name="tag.name">
                        </el-tab-pane>
                    </el-tabs>
                </div>
                <div class="router-card">
                    <router-view v-slot="{ Component }">
                        <keep-alive>
                            <component :is="Component" />
                        </keep-alive>
                    </router-view>
                </div>
            </el-main>
        </el-container>
    </el-container>
</template>
<script setup lang="ts">
import { computed, onMounted, ref } from 'vue';
import SideMenu from './SideMenu.vue';
import HeaderView from './HeaderView.vue';
import { LocalService } from '@/service/common'
import useApp from '_/store/app';
import proxy from '../../proxy'
import { onBeforeRouteUpdate } from 'vue-router'

const appStore = useApp()

const localService = new LocalService()

const handleOpen = () => { }
const handleClose = () => { }

const menus = computed(() => appStore.routers)

const menuCollapse = computed(() => appStore.menuCollapse)

const currentRouteName = ref('')


const tabClick = (vc) => {
    const name = vc.props.name
    const to = appStore.storeRouters.find(res => res.name === name)
    $router.push(to)
}

const tabClose = (name) => {
    appStore.storeRouters = appStore.storeRouters.filter(res => res.name !== name)
    const len = appStore.storeRouters.length
    if (len > 0) {
        const to = appStore.storeRouters[len - 1]
        $router.push(to)
    } else {
        $router.push({ name: 'Home' })
    }
}

onMounted(() => {
    localService.saveApiConfig({
        url: proxy['/api'].target,
        tokenKey: 'Authorization'
    })
    if ($route.name !== 'Home') {
        appStore.storeRouters = [{ ...$route }]
        currentRouteName.value = $route.name
    }

    console.log(appStore.routers);

})

onBeforeRouteUpdate((to, from) => {
    console.log('当前:', to.name, '来自:', from.name);
    const hasRoute = appStore.storeRouters.some(res => res.path === to.path)
    if (!hasRoute && to.name !== 'Home') {
        appStore.storeRouters.push(to)
    }
    currentRouteName.value = to.name
})
</script>
<style lang="scss" scoped>
.router-tabs {
    display: flex;
    align-items: center;
    background-color: #fff;
    height: 40px;
    padding: 0 10px;
    box-shadow: 0 2px 2px #e5e5e5;
    margin-bottom: 2px;
    overflow-x: auto;
    padding-top: 4px;

    :deep(.el-tabs__header) {
        margin: 0;
        border: 0;

        .el-tabs__nav {
            border: 0;
        }

        .el-tabs__item {
            border: 0;

            &.is-active {
                background-color: #2773ff11;
                border-radius: 16px 16px 0 0;
            }
        }
    }

    .header-tabs {
        width: 100%;
    }
}


.router-card {
    background-color: #f1f4f7;
    padding: 20px;
    height: calc(100vh - 1.02rem);
    overflow-y: auto;
}

.side-part {
    display: flex;
    flex-direction: column;
    height: 100%;

    .title {
        height: 60px;
        display: flex;
        align-items: center;
        justify-content: center;
    }

    .title-text {
        color: #fff;
    }

    .el-menu-vertical {
        height: 0;
        flex: 1;
        overflow-y: auto;
    }
}
</style>